//
//  HGSeletedView.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/6.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGSeletedView.h"
#import "HGOrderCell.h"
#import "HGAddressModel.h"

@interface HGSeletedView ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;

@property (strong, nonatomic) UIView *foodView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation HGSeletedView 

+ (instancetype)shareSeletedView {
    return [[NSBundle mainBundle] loadNibNamed:@"HGSeletedView" owner:nil options:nil].lastObject;
}
- (IBAction)dismiss:(id)sender {
    WS(weakSelf)
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
        weakSelf.bottom.constant = 300;
        [self layoutIfNeeded];
    }];
}

- (void)begain {
    WS(weakSelf)
    self.alpha = 1;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.bottom.constant = 0;
        [self layoutIfNeeded];
    }];
    
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.frame = App_Bounds;
        self.alpha = 0;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self initSubViews];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_titleLabel.text isEqualToString:@"配送方式"]) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"123"];
        cell.textLabel.text = _distribution[indexPath.row];
        return cell;
    } else {
        HGOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGOrderAddressCell"];
        cell.model = _addressModels[indexPath.row];
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _addressModels.count == 0 ? _distribution.count : _addressModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_distribution.count != 0) {
        if (_callDisbtuion) {
            NSArray *arr = @[@"2",@"3",@"1"];
            _callDisbtuion([arr[indexPath.row] integerValue]);
        }
    } else {
        
        [_addressModels enumerateObjectsUsingBlock:^(HGAddressModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isDefault = idx == indexPath.row ? YES : NO;
        }];
        
        [self.tableView reloadData];
        
        if (_callBack) {
            _callBack(_addressModels[indexPath.row]);
        }
    }
    
    [self dismiss:[self viewWithTag:10]];
}

- (void)setAddressModels:(NSArray<HGAddressModel *> *)addressModels {
    _addressModels = addressModels;
    _tableView.rowHeight = 64;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView reloadData];
}

- (void)setDistribution:(NSArray *)distribution {
    _distribution = distribution;
    _tableView.rowHeight = 48;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [_tableView reloadData];
}

- (void)initSubViews {
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 64;
    _tableView.tableFooterView = self.foodView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[HGOrderAddressCell class] forCellReuseIdentifier:@"HGOrderAddressCell"];
}

- (void)setIsFoodView:(BOOL)isFoodView {
    _isFoodView = isFoodView;
    _foodView.hidden = isFoodView;
}

- (UIView *)foodView {
    if (!_foodView) {
        _foodView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 128.5, _foodView.height)];
        [button setImage:[UIImage imageNamed:@"add"] forState:(UIControlStateNormal)];
        [button setTitle:@"  新建收货地址" forState:(UIControlStateNormal)];
        [button setTitleColor:kColorDefult forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(newAddress) forControlEvents:(UIControlEventTouchUpInside)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [_foodView addSubview:button];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(14, _foodView.height, kScreenWidth - 14, 1)];
        lineView.backgroundColor = kColorBackground;
        [_foodView addSubview:lineView];
    }
    return _foodView;
}

- (void)newAddress {
    if (_addAddress) {
        _addAddress();
    }
}

@end
